/**
 * Copyright 2019-2021 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "src/common/ops/populate/populate_register.h"
#include "nnacl/fp32/activation_fp32.h"
using mindspore::schema::PrimitiveType_Activation;

namespace mindspore {
namespace lite {
OpParameter *PopulateRelu6Parameter(const void *prim) {
  if (prim == nullptr) {
    MS_LOG(ERROR) << "prim is nullptr.";
    return nullptr;
  }
  auto primitive = static_cast<const schema::Primitive *>(prim);
  auto value = primitive->value_as_Activation();
  if (value == nullptr) {
    MS_LOG(ERROR) << "value is nullptr";
    return nullptr;
  }

  auto *param = reinterpret_cast<ActivationParameter *>(malloc(sizeof(ActivationParameter)));
  if (param == nullptr) {
    MS_LOG(ERROR) << "malloc ActivationParameter failed.";
    return nullptr;
  }
  (void)memset(param, 0, sizeof(ActivationParameter));

  param->op_parameter_.type_ = primitive->value_type();
  param->type_ = static_cast<int>(value->activation_type());
  param->alpha_ = value->alpha();
  param->min_val_ = value->min_val();
  param->max_val_ = value->max_val();
  param->approximate_ = value->approximate();

  std::set<int> activation_types = {
    schema::ActivationType_RELU,    schema::ActivationType_RELU6,    schema::ActivationType_LEAKY_RELU,
    schema::ActivationType_SIGMOID, schema::ActivationType_TANH,     schema::ActivationType_SWISH,
    schema::ActivationType_HSWISH,  schema::ActivationType_HSIGMOID, schema::ActivationType_HARD_TANH,
    schema::ActivationType_GELU,    schema::ActivationType_SOFTPLUS, schema::ActivationType_ELU};
  if (activation_types.find(param->type_) == activation_types.end()) {
    MS_LOG(ERROR) << "invalid activation type: " << param->type_;
    free(param);
    return nullptr;
  }
  return reinterpret_cast<OpParameter *>(param);
}

REG_POPULATE(PrimitiveType_Activation, PopulateRelu6Parameter, SCHEMA_CUR)
}  // namespace lite
}  // namespace mindspore
